import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

const modeuleFn = require.context('./routes',false,/.js$/i)

let routes = modeuleFn.keys().reduce((prev,curr) => {
  const module = modeuleFn(curr).default
  if(Array.isArray(module)){
    prev.push(...module)
  }else{
    prev.push(module)
  }
  return prev
},[])

Vue.use(VueRouter)






const router = new VueRouter({
  mode:'history',
  routes
})

router.beforeEach((to,from,next) => {
  if(to.path !== '/login'){
    if(store.getters.token == ''){
      next({ path:'/login',replace:true })
    }else{
      next()
    }
  }else{
    next()
  }
})

export default router
